#!/bin/bash

# Learn Double Examples Build Script
# Usage: ./build.sh [clean|debug|release|specific_target]

set -e  # Exit on error

BUILD_DIR="../build"
BIN_DIR="$BUILD_DIR/bin/learn_double"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Function to build all examples
build_all() {
    local build_type=$1
    print_info "Building all learn_double examples in $build_type mode..."
    
    # Go to parent directory for CMake
    cd ..
    mkdir -p build
    
    cmake -B build -DCMAKE_BUILD_TYPE=$build_type
    cmake --build build --target all_examples --parallel
    
    cd learn_double
    print_success "Build completed! Executables are in $BIN_DIR/"
    
    if [ -d "$BIN_DIR" ]; then
        print_info "Available executables:"
        ls -la $BIN_DIR/
    fi
}

# Function to build specific target
build_target() {
    local target=$1
    local build_type=${2:-Debug}
    print_info "Building specific target: $target in $build_type mode..."
    
    # Go to parent directory for CMake
    cd ..
    mkdir -p build
    
    cmake -B build -DCMAKE_BUILD_TYPE=$build_type
    cmake --build build --target $target --parallel
    
    cd learn_double
    print_success "Target $target built successfully!"
}

# Function to clean build
clean_build() {
    print_info "Cleaning build directory..."
    if [ -d "$BUILD_DIR" ]; then
        rm -rf $BUILD_DIR
        print_success "Parent project build directory cleaned!"
    else
        print_warning "Build directory doesn't exist, nothing to clean."
    fi
}

# Function to run a specific example
run_example() {
    local example=$1
    local executable="$BIN_DIR/$example"
    
    if [ -f "$executable" ]; then
        print_info "Running $example..."
        echo "==================== OUTPUT ===================="
        $executable
        echo "==================== END ======================="
    else
        print_error "Executable $executable not found! Please build first."
        exit 1
    fi
}

# Function to show available examples
show_examples() {
    print_info "Available learn_double examples:"
    if [ -f "CMakeLists.txt" ]; then
        # Extract source files from SOURCES variable and derive target names
        sed -n '/set(SOURCES/,/)/p' CMakeLists.txt | grep '\.cpp$' | sed 's/.*\///; s/\.cpp$//' | sed 's/^/  - /' | sort
    else
        print_error "CMakeLists.txt not found in learn_double directory!"
        return 1
    fi
}

# Function to show help
show_help() {
    echo "Learn Double Examples Build Script"
    echo
    echo "Usage: $0 [COMMAND] [TARGET]"
    echo
    echo "Commands:"
    echo "  clean           Clean build directory"
    echo "  debug           Build all examples in Debug mode (default)"
    echo "  release         Build all examples in Release mode"
    echo "  build [target]  Build specific target in Debug mode"
    echo "  run [target]    Run specific example"
    echo "  list            List available examples"
    echo "  help            Show this help"
    echo
    echo "Examples:"
    echo "  $0 debug                          # Build all in debug mode"
    echo "  $0 release                        # Build all in release mode"
    echo "  $0 build check_0075_save_more     # Build specific example"
    echo "  $0 run check_0075_save_more       # Run specific example"
    echo "  $0 clean                          # Clean build files"
    echo
}

# Main script logic
case "${1:-debug}" in
    "clean")
        clean_build
        ;;
    "debug")
        build_all "Debug"
        ;;
    "release")
        build_all "Release"
        ;;
    "build")
        if [ -z "$2" ]; then
            print_error "Please specify a target to build"
            show_examples
            exit 1
        fi
        build_target "$2" "Debug"
        ;;
    "run")
        if [ -z "$2" ]; then
            print_error "Please specify an example to run"
            show_examples
            exit 1
        fi
        run_example "$2"
        ;;
    "list")
        show_examples
        ;;
    "help"|"-h"|"--help")
        show_help
        ;;
    *)
        print_error "Unknown command: $1"
        show_help
        exit 1
        ;;
esac
